package gui;

import ge.util.Validadores;
import ge.escola.Escola;
import java.awt.Color;
import java.awt.Component;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;



/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class AlteraEscola extends javax.swing.JPanel {
	

	/** Creates new form primeiroAcesso */
	public AlteraEscola() {
		initComponents();

	}


	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		this.setLayout(null);

		setMinimumSize(new java.awt.Dimension(800, 600));
		setPreferredSize(new java.awt.Dimension(800, 600));
		this.setBackground(new java.awt.Color(204,204,255));
		{
			gerenciadorKamalvin = new JLabel();
			this.add(gerenciadorKamalvin);
			gerenciadorKamalvin.setText("Altera Dados da Escola");
			gerenciadorKamalvin.setBounds(193, 12, 387, 48);
			gerenciadorKamalvin.setFont(new java.awt.Font("Arial",0,36));
			gerenciadorKamalvin.setForeground(new java.awt.Color(0,102,102));
			gerenciadorKamalvin.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
		}
		{
			escolaNome = new JLabel();
			this.add(escolaNome);
			escolaNome.setText("Nome da Escola");
			escolaNome.setBounds(67, 96, 180, 25);
			escolaNome.setFont(new java.awt.Font("Arial",0,24));
			escolaNome.setForeground(new java.awt.Color(0,102,102));
		}
		{
			endereco = new JLabel();
			this.add(endereco);
			endereco.setText("Endereco");
			endereco.setBounds(67, 133, 105, 31);
			endereco.setFont(new java.awt.Font("Arial",0,24));
			endereco.setForeground(new java.awt.Color(0,102,102));
		}
		{
			nucleo = new JLabel();
			this.add(nucleo);
			nucleo.setText("Nucleo de Ensino");
			nucleo.setBounds(67, 181, 194, 19);
			nucleo.setFont(new java.awt.Font("Arial",0,24));
			nucleo.setForeground(new java.awt.Color(0,102,102));
		}
		{
			anoFundacao = new JLabel();
			this.add(anoFundacao);
			anoFundacao.setText("Ano da Fundacao");
			anoFundacao.setBounds(67, 223, 196, 25);
			anoFundacao.setFont(new java.awt.Font("Arial",0,24));
			anoFundacao.setForeground(new java.awt.Color(0,102,102));
		}
		{
			telefone = new JLabel();
			this.add(telefone);
			telefone.setText("Telefone");
			telefone.setBounds(67, 264, 96, 22);
			telefone.setFont(new java.awt.Font("Arial",0,24));
			telefone.setForeground(new java.awt.Color(0,102,102));
		}
		{
			email = new JLabel();
			this.add(email);
			email.setText("Email");
			email.setBounds(67, 307, 67, 22);
			email.setFont(new java.awt.Font("Arial",0,24));
			email.setForeground(new java.awt.Color(0,102,102));
		}
		{
			duracao = new JLabel();
			this.add(duracao);
			duracao.setText("Duracao da Aula");
			duracao.setBounds(67, 350, 182, 25);
			duracao.setFont(new java.awt.Font("Arial",0,24));
			duracao.setForeground(new java.awt.Color(0,102,102));
		}
		{
			campoNome = new JTextField();
			this.add(campoNome);
			campoNome.setBounds(283, 100, 333, 22);
			campoNome.setFont(new java.awt.Font("Arial",0,18));
			if(GerenciadorDeJanelas.escola.getNome()==null){
			    campoNome.setText("");}
			else{
				campoNome.setText(GerenciadorDeJanelas.escola.getNome());
			}
			campoNome.addKeyListener(new java.awt.event.KeyAdapter() {
	            public void keyReleased(java.awt.event.KeyEvent evt) {
	                caixaNomeKeyReleased(evt);
	            }
	        });
		}
		{
			campoEndereco = new JTextField();
			this.add(campoEndereco);
			campoEndereco.setBounds(283, 142, 333, 22);
			campoEndereco.setFont(new java.awt.Font("Arial",0,18));
			campoEndereco.setText(GerenciadorDeJanelas.escola.getEndereco());
			campoEndereco.addKeyListener(new java.awt.event.KeyAdapter() {
	            public void keyReleased(java.awt.event.KeyEvent evt) {
	                caixaEnderecoKeyReleased(evt);
	            }
	        });
		}
		{
			campoNucleo = new JTextField();
			this.add(campoNucleo);
			campoNucleo.setBounds(283, 184, 333, 22);
			campoNucleo.setFont(new java.awt.Font("Arial",0,18));
			campoNucleo.setText(GerenciadorDeJanelas.escola.getNucleoDeEducacao());
			campoNucleo.addKeyListener(new java.awt.event.KeyAdapter() {
	            public void keyReleased(java.awt.event.KeyEvent evt) {
	                caixaNucleoKeyReleased(evt);
	            }
	        });
		}
		{
			campoFundacao = new JTextField();
			this.add(campoFundacao);
			campoFundacao.setBounds(283, 227, 333, 22);
			campoFundacao.setFont(new java.awt.Font("Arial",0,18));
			campoFundacao.setText(GerenciadorDeJanelas.escola.getAnoFundacao());
			campoFundacao.addKeyListener(new java.awt.event.KeyAdapter() {
	            public void keyReleased(java.awt.event.KeyEvent evt) {
	            	caixaAnoFundKeyReleased(evt);
	            }
	        });
		}
		{
			telUm = new JTextField();
			this.add(telUm);
			telUm.setText("DDD");
			telUm.setBounds(205, 267, 44, 27);
			telUm.setFont(new java.awt.Font("Arial",0,18));
			telUm.setText(GerenciadorDeJanelas.escola.getFragmento(1));
			telUm.addKeyListener(new java.awt.event.KeyAdapter() {
	            public void keyReleased(java.awt.event.KeyEvent evt) {
	            	caixaTelefoneKeyReleased(evt);
	            }
	        });
		}
		{
			telDois = new JTextField();
			this.add(telDois);
			telDois.setText("XXXX");
			telDois.setBounds(268, 267, 52, 27);
			telDois.setFont(new java.awt.Font("Arial",0,18));
			telDois.setText(GerenciadorDeJanelas.escola.getFragmento(2));
			telDois.addKeyListener(new java.awt.event.KeyAdapter() {
	            public void keyReleased(java.awt.event.KeyEvent evt) {
	            	caixaTelefoneKeyReleased(evt);
	            }
	        });
		}
		{
			telTres = new JTextField();
			this.add(telTres);
			telTres.setText("XXXX");
			telTres.setBounds(338, 267, 52, 27);
			telTres.setFont(new java.awt.Font("Arial",0,18));
			telTres.setText(GerenciadorDeJanelas.escola.getFragmento(3));
			telTres.addKeyListener(new java.awt.event.KeyAdapter() {
	            public void keyReleased(java.awt.event.KeyEvent evt) {
	            	caixaTelefoneKeyReleased(evt);
	            }
	        });
		}
		{
			campoEmail = new JTextField();
			this.add(campoEmail);
			campoEmail.setBounds(205, 312, 411, 22);
			campoEmail.setFont(new java.awt.Font("Arial",0,18));
			campoEmail.setText(GerenciadorDeJanelas.escola.getEmail());
			campoEmail.addKeyListener(new java.awt.event.KeyAdapter() {
	            public void keyReleased(java.awt.event.KeyEvent evt) {
	            	caixaEmailKeyReleased(evt);
	            }
	        });
		}
		{
			campoDuracaoUm = new JTextField();
			this.add(campoDuracaoUm);
			campoDuracaoUm.setText("HH");
			campoDuracaoUm.setBounds(280, 352, 35, 26);
			campoDuracaoUm.setFont(new java.awt.Font("Arial",0,18));
			campoDuracaoUm.setText(GerenciadorDeJanelas.escola.getFragmentoHora(1));
			campoDuracaoUm.addKeyListener(new java.awt.event.KeyAdapter() {
	            public void keyReleased(java.awt.event.KeyEvent evt) {
	            	caixaDuracaoKeyReleased(evt);
	            }
	        });
		}
		{
			campoDoisPontos = new JLabel();
			this.add(campoDoisPontos);
			campoDoisPontos.setText(":");
			campoDoisPontos.setBounds(327, 353, 10, 19);
			campoDoisPontos.setFont(new java.awt.Font("Arial",0,24));
			campoDoisPontos.setForeground(new java.awt.Color(0,102,102));
		}
		{
			campoDuracaoDois = new JTextField();
			this.add(campoDuracaoDois);
			campoDuracaoDois.setText("MM");
			campoDuracaoDois.setBounds(344, 352, 37, 27);
			campoDuracaoDois.setFont(new java.awt.Font("Arial",0,18));
			campoDuracaoDois.setText(GerenciadorDeJanelas.escola.getFragmentoHora(2));
			campoDuracaoDois.addKeyListener(new java.awt.event.KeyAdapter() {
	            public void keyReleased(java.awt.event.KeyEvent evt) {
	            	caixaDuracaoKeyReleased(evt);
	            }
	        });
		}
		{
			alterar = new JButton();
			this.add(alterar);
			alterar.setText("Alterar");
			alterar.setBounds(277, 463, 131, 23);
			alterar.setFont(new java.awt.Font("Arial",2,18));
			alterar.setBackground(new java.awt.Color(255,255,255));
			alterar.addMouseListener(new java.awt.event.MouseAdapter() {
	            public void mouseClicked(java.awt.event.MouseEvent evt) {
	                try {
	                	alterarButtonMouseClicked(evt);
					} catch (Exception e) {
						e.printStackTrace();
					}
	            }
	        });
		}
		{
			botaoCancelar = new JButton();
			this.add(botaoCancelar);
			botaoCancelar.setText("Cancelar");
			botaoCancelar.setBounds(475, 463, 131, 24);
			botaoCancelar.setFont(new java.awt.Font("Arial",2,18));
			botaoCancelar.setBackground(new java.awt.Color(255,255,255));
			botaoCancelar.addMouseListener(new java.awt.event.MouseAdapter() {
	            public void mouseClicked(java.awt.event.MouseEvent evt) {
	                try {
	                	cancelarButtonMouseClicked(evt);
					} catch (Exception e) {
						e.printStackTrace();
					}
	            }
	        });
		}
		{
			checaNome = new JLabel();
			this.add(checaNome);
			checaNome.setBounds(651, 101, 63, 19);
			checaNome.setFont(new java.awt.Font("Arial",0,18));
		}
		{
			checaEndereco = new JLabel();
			this.add(checaEndereco);
			checaEndereco.setBounds(651, 146, 63, 15);
			checaEndereco.setFont(new java.awt.Font("Arial",0,18));
		}
		{
			checaNucleo = new JLabel();
			this.add(checaNucleo);
			checaNucleo.setBounds(651, 188, 63, 15);
			checaNucleo.setFont(new java.awt.Font("Arial",0,18));
		}
		{
			checaFundacao = new JLabel();
			this.add(checaFundacao);
			checaFundacao.setBounds(651, 231, 63, 15);
			checaFundacao.setFont(new java.awt.Font("Arial",0,18));
		}
		{
			checaTelefone = new JLabel();
			this.add(checaTelefone);
			checaTelefone.setBounds(408, 273, 64, 15);
			checaTelefone.setFont(new java.awt.Font("Arial",0,18));
		}
		{
			checaEmail = new JLabel();
			this.add(checaEmail);
			checaEmail.setBounds(651, 315, 63, 15);
			checaEmail.setFont(new java.awt.Font("Arial",0,18));
		}
		{
			checaDuracao = new JLabel();
			this.add(checaDuracao);
			checaDuracao.setBounds(408, 359, 64, 15);
			checaDuracao.setFont(new java.awt.Font("Arial",0,18));
		}
		{
			campoUm = new JLabel();
			this.add(campoUm);
			campoUm.setBounds(43, 80, 712, 351);
			campoUm.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
		}

	}// </editor-fold>

	private void caixaNomeKeyReleased(java.awt.event.KeyEvent evt) {                                      
        String nome = campoNome.getText();
        if(!Validadores.validaNome(nome)){
            checaNome.setForeground(Color.RED);
            checaNome.setText("Invalido");
            nomeValido = false;
        }else{
        	checaNome.setForeground(Color.GREEN);
            checaNome.setText("Ok");
            nomeValido = true;
        }
    }
	
	private void caixaAnoFundKeyReleased(java.awt.event.KeyEvent evt) {                                      
        String anoFund = campoFundacao.getText();
        if(!Validadores.validaAno(anoFund)){
        	checaFundacao.setForeground(Color.RED);
            checaFundacao.setText("Invalido");
            anoFundValido = false;
        }else{
        	checaFundacao.setForeground(Color.GREEN);
            checaFundacao.setText("Ok");
            anoFundValido = true;
        }
    }
	
	private void caixaEnderecoKeyReleased(java.awt.event.KeyEvent evt) {                                      
        String endereco = campoEndereco.getText();
        if(!Validadores.validaEndereco(endereco)){
            checaEndereco.setForeground(Color.RED);
            checaEndereco.setText("Invalido");
            enderecoValido = false;
        }else{
            enderecoValido = true;
            checaEndereco.setForeground(Color.GREEN);
            checaEndereco.setText("Ok");
        }
    }
	
	private void caixaNucleoKeyReleased(java.awt.event.KeyEvent evt) {                                      
        String nucleo = campoNucleo.getText();
        if(!Validadores.validaNome(nucleo)){
            nucleoValido = false;
            checaNucleo.setForeground(Color.RED);
            checaNucleo.setText("Invalido");
        }else{
        	checaNucleo.setForeground(Color.GREEN);
            checaNucleo.setText("Ok");
            nucleoValido = true;
        }
    }
	
	private void caixaTelefoneKeyReleased(java.awt.event.KeyEvent evt) {                                      
        String DDD = telUm.getText();
        String num1 = telDois.getText();
        String num2 = telTres.getText();
        
        if(!Validadores.validaTelefone("("+DDD+")"+num1+"-"+num2)){
            checaTelefone.setText("Invalido");
            checaTelefone.setForeground(Color.RED);
            telefoneValido = false;
        }else{
        	checaTelefone.setForeground(Color.GREEN);
            checaTelefone.setText("Ok");
            telefoneValido = true;
        }
    }
	
	private void caixaEmailKeyReleased(java.awt.event.KeyEvent evt) {                                      
        String email = campoEmail.getText();
        if(!Validadores.validaEmail(email)){
            emailValido = false;
            checaEmail.setForeground(Color.RED);;
            checaEmail.setText("Invalido");
        }else{
        	checaEmail.setForeground(Color.GREEN);;
            checaEmail.setText("Ok");
            emailValido = true;
        }
    }
	
	private void caixaDuracaoKeyReleased(java.awt.event.KeyEvent evt) {                                      
        String duracao = campoDuracaoUm.getText()+":"+campoDuracaoDois.getText();
        if(!Validadores.validaHora(duracao)){
            checaDuracao.setText("Invalido");
            checaDuracao.setForeground(Color.RED);
            duracaoValido = false;
        }else{
        	checaDuracao.setForeground(Color.GREEN);
        	checaDuracao.setText("Ok");
            duracaoValido = true;
        }
    }

	private void alterarButtonMouseClicked(java.awt.event.MouseEvent evt) throws Exception{
		if(nomeValido && anoFundValido && enderecoValido && nucleoValido && telefoneValido && emailValido && duracaoValido){
			GerenciadorDeJanelas.escola = new Escola(campoNome.getText(), campoEndereco.getText(), campoNucleo.getText(), campoFundacao.getText(),"("+telUm.getText()+")"+telDois.getText()+"-"+telTres.getText(),campoEmail.getText(),campoDuracaoUm.getText()+":"+campoDuracaoDois.getText());
			GerenciadorDeJanelas.window.setVisible(false);
			GerenciadorDeJanelas.window.setContentPane(GerenciadorDeJanelas.menuAltera);
			GerenciadorDeJanelas.window.setVisible(true);
			// transforma em xml
			String xmlEscola = GerenciadorDeJanelas.ARQUIVADOR
					.toXML(GerenciadorDeJanelas.escola);
			// grava o arrayXml no arquivo
			GerenciadorDeJanelas.gravar(GerenciadorDeJanelas.arquivoEscola,
					xmlEscola);	
		}
		else{
			JOptionPane.showMessageDialog(null,"Informações da escola não alteradas. Por favor, cheque novamente as informações digitadas.");
		}
	
	}
	


	private void cancelarButtonMouseClicked(java.awt.event.MouseEvent evt) {
		GerenciadorDeJanelas.window.setVisible(false);
		GerenciadorDeJanelas.window.setContentPane(GerenciadorDeJanelas.menuAltera);
		GerenciadorDeJanelas.window.setVisible(true);
	}


	// Variables declaration - do not modify
	private boolean nomeValido = true;
	private boolean anoFundValido = true;
	private boolean enderecoValido = true;
	private boolean nucleoValido = true;
	private boolean telefoneValido = true;
	private boolean emailValido = true;
	private JLabel escolaNome;
	private JLabel campoUm;
	private JLabel checaDuracao;
	private JLabel checaEmail;
	private JLabel checaTelefone;
	private JLabel checaFundacao;
	private JLabel checaNucleo;
	private JLabel checaEndereco;
	private JLabel checaNome;
	private JButton botaoCancelar;
	private JButton alterar;
	private JTextField campoDuracaoDois;
	private JLabel campoDoisPontos;
	private JTextField campoDuracaoUm;
	private JTextField campoEmail;
	private JTextField telTres;
	private JTextField telDois;
	private JTextField telUm;
	private JTextField campoFundacao;
	private JTextField campoNucleo;
	private JTextField campoEndereco;
	private JTextField campoNome;
	private JLabel duracao;
	private JLabel email;
	private JLabel telefone;
	private JLabel anoFundacao;
	private JLabel nucleo;
	private JLabel endereco;
	private JLabel gerenciadorKamalvin;
	private boolean duracaoValido = true;
	// End of variables declaration
}
